package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/19 22:10
 */
@Service
public class DtsCouponServiceImpl implements DtsCouponService {
    @Autowired
    DtsCouponMapper couponMapper;
    @Override
    public Map<String, Object> selectByPage(Integer page, Integer limit, String sort, String order, String name, Integer type, Integer status) {
        IPage<DtsCoupon> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        if (!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        if (type!=null){
            wrapper.eq("type",type);
        }
        if (status!=null){
            wrapper.eq("status",status);
        }

        couponMapper.selectPage(iPage,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public DtsCoupon selectById(Integer id) {
        LambdaQueryWrapper<DtsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCoupon::getId,id);
        DtsCoupon coupon = couponMapper.selectOne(wrapper);
        return coupon;
    }

    @Override
    public void insert(DtsCoupon coupon) {

        couponMapper.insert(coupon);
    }

    @Override
    public void update(DtsCoupon coupon) {
        couponMapper.updateById(coupon);
    }

    @Override
    public void delete(DtsCoupon coupon) {
        couponMapper.deleteById(coupon);
    }
}
